# _*_ coding:utf-8 _*_
# @Time : 2023/5/25 17:57
# @Author : wsl
# @File : day05_demo
# @Project : python  快代理隐藏真实ip

import urllib.request
import urllib.error
import random
from pip._vendor import requests

url = 'http://www.baidu.com/s?wd=ip'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0',
    'Cookie': 'BIDUPSID=E0B05F5DA883F1CE530B0A07F740DAAC; PSTM=1627019121; __yjs_duid=1_ee462671a13aea34dc92a2e0da8be4751628506522571; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=DCE8E680EE591EAC6438D7AC4745EE63:FG=1; BDUSS=dIam9jUVZLWVBqcjBxbGVDVXZjWGVTd2g0OU1CUWtFamJsaXFuTnZEd2I0V2xrSVFBQUFBJCQAAAAAAAAAAAEAAAAxpPyJ0KG7qMn6NjY2NjYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABtUQmQbVEJka2; BDUSS_BFESS=dIam9jUVZLWVBqcjBxbGVDVXZjWGVTd2g0OU1CUWtFamJsaXFuTnZEd2I0V2xrSVFBQUFBJCQAAAAAAAAAAAEAAAAxpPyJ0KG7qMn6NjY2NjYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABtUQmQbVEJka2; APPGUIDE_10_0_2=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=DCE8E680EE591EAC6438D7AC4745EE63:FG=1; BDRCVFR[C0p6oIjvx-c]=I67x6TjHwwYf0; delPer=0; BA_HECTOR=a0a02h2k0k2g8000a18la1f71i6rl2k1n; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BDPASSGATE=IlPT2AEptyoA_yiU4SO_3lIN8eDEUsCD34OtVnti3ECGh67BmhH74Mx4E6rLTSasGUqa-YyfmqkCpjrFV6xjg0N_gRsLlCx-5CSStqmh-cfIRBZjtKpmCbDJMyYUruDxkhUaz4wT_OVEVFoKewPJpuo4isOlre2Hah40skLrg_beZVmpBXb6r7WTX76fRHvYDsi1yujEaVJAVFeBUu4NKDTucloiPy1tx_7bi3Mq3Q45rkoUGv8dL3cA1G85Jppg0PmV1QC7zMGnDkscwnxzLl1ejSa85tD6Mk9d_KvxfMpGVdPeSsCDVDrXBL1vctD1Or2iTAWZmtkGDV9S98sUBph5HsPVCXTEYjUOLdOFvAGmGpM-ulOEIwDLtIMQO10H_QlcZq5CTyoFizOfqBm4gCjo_3T_i0FvM0wOCpePu8gvmHdWBpbht57jc7Jv-p_fT7afE5XiGa4l3G2s-37DDb44K_r82VEbqUuloizSjSr9DwrTWPwJcy-77WxmTWiB9QePNo3XK3O2nr64tQHV84uz-oSUqzaZwyqfRNWpTx9DN-9zobBoInHWvYvtzHx_O4GdvI4n_VDDnD2L81uwpzB9a4A01NJKOtD0JPITwc_IopwyXC9to_Sz3wFfZ7nDv3ltB_qcX8D6GEnfSFFmpbl0ZDLmHJTzvtLgETVYEVCX0lTBBqhiw8NV5oEqPebbPDDFg3Q0PjiopwkjNNWJgmB4AvgzEi25vXzJ3-8NCRnx7xJmwEg46g1r4o8TYv0XbpUwgBlCA5_S62PUCreFoI1OspfegRZzWq_IWF8zu579FugP-nnzEFyXiSrIuQtne-Uk; H_WISE_SIDS=219946_131861_132548_219558_216837_213355_214799_219942_213028_204907_110085_236312_243873_244724_240590_245411_250137_247148_249893_251786_253427_253213_255937_255982_107315_256083_255659_256349_254831_256739_251973_254317_256940_257081_254075_257292_257660_257738_257936_257824_257586_255224_258248_258166_257994_258471_258370_258376_258724_258729_258756_258837_258938_258984_258958_230288_259048_259037_257015_259190_259193_256223_259405_259285_259391_259430_259568_259605_259625_256999_259558_259656_254469_259704_258772_234295_234208_259883_259889_259642_255909_259052_260034_254300_260123_257302_259240_256229_259721_260365_260225_259187_253022_255212_260370_260582_258081_260331_260804_260834_259408_250303_259422_259584_260976_261003_259306_261029_188332_259113_261299_261374_253900_261343_259448; H_WISE_SIDS_BFESS=219946_131861_132548_219558_216837_213355_214799_219942_213028_204907_110085_236312_243873_244724_240590_245411_250137_247148_249893_251786_253427_253213_255937_255982_107315_256083_255659_256349_254831_256739_251973_254317_256940_257081_254075_257292_257660_257738_257936_257824_257586_255224_258248_258166_257994_258471_258370_258376_258724_258729_258756_258837_258938_258984_258958_230288_259048_259037_257015_259190_259193_256223_259405_259285_259391_259430_259568_259605_259625_256999_259558_259656_254469_259704_258772_234295_234208_259883_259889_259642_255909_259052_260034_254300_260123_257302_259240_256229_259721_260365_260225_259187_253022_255212_260370_260582_258081_260331_260804_260834_259408_250303_259422_259584_260976_261003_259306_261029_188332_259113_261299_261374_253900_261343_259448; SE_LAUNCH=5%3A1684998827; PSINO=5; H_PS_PSSID=38516_36543_38673_38617_38591_38595_38375_38639_26350_22160; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1683355490,1683788644,1685000899; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1685000913; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1685003495; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1685003516; ab_sr=1.0.1_NzhlNWQ1NDhhNDdjOTAxOGY3MWZhYTY1YTVkNGQxY2Q3NjY5YjVjNWJlZjQ4OGY0OWFiYWM4MjcxOWNkNjc3MTdlYzIxOWYxYjA2MzdjMWI4NmQ1ZTA0ZDMxNWYzM2ZiOWQxZWEwODY1NzEwNGIzNjcyY2E5YjQzYjY1N2UyNTExN2EyOWQxMWQyYTliMzU3M2U2N2M5ZGMyMTkyZDMwOGRmNjhhNWFjOWIzMGUyZjhhNjUzYmE4OTEyOWRhMWE2',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',

}

# 提取代理API接口，获取1个代理IP
api_url = "https://kps.kdlapi.com/api/getkps/?secret_id=okltotzhlvlldhmipxbx&num=1&signature=5e4mhxopbtg059ltsyy4tw2zyw6160jt&pt=1&sep=1"
proxy_ip = requests.get(api_url).text
user = "wangshali_sh"
pwd = "0satrsyk"

# proxies = {'http': '121.41.8.23:16818'}
#
# # 代理池
# proxies_pool = [
#     {'http': '121.41.8.23:16818'},
#     {'http': '121.41.8.23:16818'}
# ]
#
# proxies = random.choice(proxies_pool)

proxies = {
    "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": user, "pwd": pwd, "proxy": proxy_ip},
    "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": user, "pwd": pwd, "proxy": proxy_ip}
}

request = urllib.request.Request(url=url, headers=headers)
handler = urllib.request.ProxyHandler(proxies=proxies)
opener = urllib.request.build_opener(handler)
response = opener.open(request)
content = response.read().decode('utf‐8')
with open('../../resource/image/output/proxy.html', 'w', encoding='utf‐8')as fp:
    fp.write(content)
